package com.util

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.*
import android.net.Uri
import android.os.Build
import android.widget.LinearLayout
import androidx.core.content.FileProvider
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.File
import java.net.URL
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
import java.util.zip.ZipOutputStream


class util {
    companion object{
        @JvmStatic
        fun  scaleBitmaps(origin : Bitmap, ratio:Float):Bitmap{
            if(origin==null)return origin;
            val matrix= Matrix()
            matrix.preScale(ratio,ratio)
            val newBM=Bitmap.createBitmap(origin, 0, 0, origin.width, origin.height, matrix, false)
            if(newBM.equals(origin)){
                return newBM
            }
            origin.recycle()
            return newBM
        }
        @JvmStatic
        fun setSize(linearLayout : LinearLayout,width :Int,height:Int){
            val layout=linearLayout.layoutParams
            if(height!=0){
                layout.height=height
            }
            if(width!=0){
                layout.width=width;
            }
            linearLayout.layoutParams = layout
        }
        @JvmStatic
        fun createCircleImage(source:Bitmap):Bitmap{
            val min=Math.min(source.width,source.height)
            val paint=Paint()
            paint.isAntiAlias = true
            val target=Bitmap.createBitmap(min,min,Bitmap.Config.ARGB_8888)
            val canvas=Canvas(target)
            canvas.drawCircle((min/2).toFloat(), (min/2).toFloat(), (min/3).toFloat(),paint)
            paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
            canvas.drawBitmap(source,(0).toFloat(),(0).toFloat(),paint)
            return target
        }
        @JvmStatic
        fun scaleBitmap(origin: Bitmap,newWith: Int,newHeight:Int,context: Context):Bitmap{
            val scaleWidth=(newWith).toFloat()/origin.width
            val scaleHeight=(newHeight).toFloat()/origin.height
            val matrix=Matrix()
            matrix.postScale(scaleWidth,scaleHeight)
            val  newBM=Bitmap.createBitmap(origin,0,0, origin.width,origin.height,matrix,false)
            if(!origin.isRecycled){
                origin.recycle()
            }
            return newBM
        }
        @JvmStatic
        fun getBitmap (s:String):Bitmap{
            val url = URL(s)
            return BitmapFactory.decodeStream(url.openStream())
        }
        @JvmStatic
        fun getPicFromBytes(bytes: ByteArray,opts:BitmapFactory.Options): Bitmap? {
            return BitmapFactory.decodeByteArray(bytes,0,bytes.size,opts)

        }
        @JvmStatic
        fun installApk(activity: Activity,apkPath:String){
            val intent=Intent(Intent.ACTION_VIEW)
            if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){
                intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
                val contentURL= FileProvider.getUriForFile(activity,"com.xiaohu.gps.FileProvider",File(apkPath))
                intent.setDataAndType(contentURL,"application/vnd.android.package-archive")
            }else{
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                intent.setDataAndType(Uri.fromFile(File(apkPath)),"application/vnd.android.package-archive")
            }
            activity.startActivity(intent)
        }
        /***
         * 压缩Zip
         *
         * @param data
         * @return
         */
        @JvmStatic
        fun zip(data: ByteArray): ByteArray? {
            var b: ByteArray? = null
            try {
                val bos = ByteArrayOutputStream()
                val zip = ZipOutputStream(bos)
                val entry = ZipEntry("zip")
                entry.size = data.size.toLong()
                zip.putNextEntry(entry)
                zip.write(data)
                zip.closeEntry()
                zip.close()
                b = bos.toByteArray()
                bos.close()
            } catch (ex: Exception) {
                ex.printStackTrace()
            }
            return b
        }

        /***
         * 解压Zip
         *
         * @param data
         * @return
         */
        @JvmStatic
        fun unZip(data: ByteArray?): ByteArray? {
            var b: ByteArray? = null
            try {
                val bis = ByteArrayInputStream(data)
                val zip = ZipInputStream(bis)
                while (zip.nextEntry != null) {
                    val buf = ByteArray(1024)
                    var num = -1
                    val baos = ByteArrayOutputStream()
                    while (zip.read(buf, 0, buf.size).also { num = it } != -1) {
                        baos.write(buf, 0, num)
                    }
                    b = baos.toByteArray()
                    baos.flush()
                    baos.close()
                }
                zip.close()
                bis.close()
            } catch (ex: Exception) {
                ex.printStackTrace()
            }
            return b
        }
    }
}